﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class MP_FieldDuties : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        private IClassBLL classBLL = new ClassBLL();
        private IFieldBLL fieldBLL = new FieldBLL();
        private IFieldDutyBLL dutyBLL = new FieldDutyBLL();
        private IFieldDutyDtlsBLL dutyDtlsBll = new FieldDutyDtlsBLL();
        private PrimaryKeyBLL pkBLL = new PrimaryKeyBLL();
        private ITreeViewBLL treeBLL = new TreeViewBLL(); 

        private PrimaryKeyInfo pkInfo;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                RadTreeNode selected_node = RadTreeView1.SelectedNode;

                Panel4.Visible = false;
                //PanelGrid.Visible = selected_node != null;
            }
        }       
        public void LoadHeader ()
        {
            IList<FieldInfo> fieldInfo = fieldBLL.ListFields(lblFieldSeq.Text);
            IList<ClassInfo> classInfo = classBLL.ListClassesBySequence(Int32.Parse(lblClassSeq.Text));
            //pkInfo = pkBLL.GetFieldDutyPK(fieldInfo[0].Sequence, classInfo[0].Sequence);
            //int fdSeq = Int32.Parse(pkInfo.Seq.ToString());

            //IList<FieldDutyInfo> fdList = dutyBLL.ListFieldDutiesBySequence(fdSeq);

            lblPosTypeName.Text = fieldInfo[0].PositionTypes.Name;
            lblFieldName.Text = fieldInfo[0].Name;
            lblLineName.Text = fieldInfo[0].LineName;
            lblClassName.Text = classInfo[0].Name;
        }
        public void LoadDuty()
        {
            IList<FieldDutyInfo> infoList = dutyBLL.ListFieldDuties(lblFieldSeq.Text.Trim(), lblClassSeq.Text.Trim());
            if (infoList.Count > 0)
            {
                lblSeq.Text = infoList[0].Sequence.ToString();
                lblDescr.Text = infoList[0].Description;
                txtDescr.Text = infoList[0].Description;
            }
            else 
            {
                lblSeq.Text = "";
                lblDescr.Text = "";
                txtDescr.Text = "";
            }

            txtDescr.Visible = false;
            lblDescr.Visible = !txtDescr.Visible;
            btnEdit.Visible = true;
            btnCancel.Visible = false;
            btnAddNew.Visible = false;
            btnUpdate.Visible = false;
        }


        private void LoadRootNodes(RadTreeView treeView, TreeNodeExpandMode expandMode)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadFieldTreeViewForDuty(1, "");

            foreach (TreeViewInfo info in treeBLL.LoadFieldTreeViewForDuty(1, ""))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                treeView.Nodes.Add(node);
            }
        }

        private void PopulateNodeOnDemand(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            e.Node.Dispose();
            e.Node.Nodes.Clear();

            foreach (TreeViewInfo info in treeBLL.LoadFieldTreeViewForDuty(1, e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Expanded = true;
            e.Node.ExpandMode = expandMode;
        }

        public void PopulateNodeOnEditGrid(RadTreeNodeEventArgs e, TreeNodeExpandMode expandMode)
        {
            foreach (TreeViewInfo info in treeBLL.LoadFieldTreeViewForDuty(1, e.Node.Value))
            {
                RadTreeNode node = new RadTreeNode();
                node.Text = info.Name;
                node.ToolTip = info.Descr;
                node.Value = info.Id;
                node.ImageUrl = info.Image_url;
                if (Convert.ToInt32(info.Children_count) > 0)
                {
                    node.ExpandMode = expandMode;
                }
                e.Node.Nodes.Add(node);
            }

            e.Node.Nodes.Clear();
            e.Node.ExpandMode = expandMode;
        }

        protected void btnExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadFieldTreeViewForDuty(1, "");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IFieldDutyTreeBLL bll = new FieldDutyTreeBLL();

            //RadTreeView1.Nodes.Clear();
            //if (txtSearch.Text.Length > 0)
            //{
            //    RadTreeView1.DataSource = bll.GetHashCode(txtSearch.Text);
            //    RadTreeView1.DataBind();
            //    RadTreeView1.ExpandAllNodes();
            //    lblResult.Text = RadTreeView1.GetAllNodes().Count.ToString();
            //    PanelSearch.Visible = true;
            //}
            //else
            //{
            //    LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
            //    PanelSearch.Visible = false;
            //}
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            LoadDuty();
        }

        protected void RadTreeView1_NodeClick(object sender, RadTreeNodeEventArgs e)
        {
            RadTreeNode selected_node = RadTreeView1.SelectedNode;

            string value = selected_node.Value;
            string[] root_value = selected_node.Value.Split(new string[] { "-" }, StringSplitOptions.None);

                if (!string.IsNullOrEmpty(value))
            {
                Panel4.Visible = selected_node.Level == 1;  // Panel4.Visible = selected_node.Level == 2;
                //PanelGrid.Visible = selected_node.Level == 2;

                if (selected_node.Level == 1)   //(selected_node.Level == 2)
                {
                    Session["FieldSeq"] = root_value[0];
                    Session["ClassSeq"] = root_value[1];
                    lblFieldSeq.Text = root_value[0];
                    lblClassSeq.Text = root_value[1];
                    LoadHeader();
                    LoadDuty();
                    LoadDataGrid();                    
                }

            }

        }

        protected void btnCollapse_Click(object sender, EventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            FieldDutyDtlsInfo Info = new FieldDutyDtlsInfo();
            Session["FieldSeq"] = lblFieldSeq.Text;
            Session["ClassSeq"] = lblClassSeq.Text;

            if (e.CommandName == "Delete")
            {
                string seq = (e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["field_duty_dtls_seq"].ToString();

                dutyDtlsBll.Delete(Int32.Parse(seq), ssMng.EmpSeqI);
            }

            LoadDataGrid();
        }

        public void LoadDataGrid()
        {
            Nullable<int> dutySeq = lblSeq.Text.Trim().Length > 0 ? Int32.Parse(lblSeq.Text.Trim()) : 0;

            //IList<FieldDutyDtlsInfo> Info = dutyDtlsBll.ListFieldDutyDetails(dutySeq);

            DataSet ds = dutyDtlsBll.LoadFieldDutyDetails((int?)null, dutySeq);
            DataTable dt = null;

            if (ds.Tables.Count > 0)
            {
                dt = ds.Tables[0];
            }

            RadGrid1.DataSource = dt;

            GridGroupByExpression g = new GridGroupByExpression(RadGrid1.Columns[4]);
            //GridSortExpression expression = new GridSortExpression();
            //expression.FieldName = "Duty.Sequence";
            //expression.SetSortOrder("Ascending");
            
            RadGrid1.MasterTableView.GroupByExpressions.Add(g);            
            RadGrid1.DataBind();

            //RadGrid1.MasterTableView.SortExpressions.AddSortExpression(expression);
            //RadGrid1.Rebind();
        }
        protected void cmdSearch_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.GetPositionResultSearch(1, txtSearch.Text);

            RadTreeView1.Nodes.Clear();
            if (txtSearch.Text.Length > 0)
            {
                RadTreeView1.DataSource = infoList;
                RadTreeView1.DataBind();
                RadTreeView1.ExpandAllNodes();
            }
            else
            {
                LoadRootNodes(RadTreeView1, TreeNodeExpandMode.ServerSideCallBack);
                //PanelSearch.Visible = false;
            }
        }

        protected void cmdExpand_Click(object sender, ImageClickEventArgs e)
        {
            IList<TreeViewInfo> infoList = treeBLL.LoadPositionTreeView(1, "");

            RadTreeView1.Nodes.Clear();
            RadTreeView1.DataSource = infoList;
            RadTreeView1.DataBind();
            RadTreeView1.ExpandAllNodes();
        }

        protected void cmdCollapse_Click(object sender, ImageClickEventArgs e)
        {
            RadTreeView1.CollapseAllNodes();
        }

        protected void RadTreeView1_NodeExpand(object sender, RadTreeNodeEventArgs e)
        {
            PopulateNodeOnDemand(e, TreeNodeExpandMode.ServerSideCallBack);

        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            lblDescr.Visible = false;
            txtDescr.Visible = true;
            btnAddNew.Visible = lblSeq.Text.Trim().Length <= 0;
            btnUpdate.Visible = !btnAddNew.Visible;
            btnEdit.Visible = false;
            btnCancel.Visible = true;
        }

        protected void btnAddNew_Click(object sender, EventArgs e)
        {
            dutyBLL.Insert(Session["FieldSeq"].ToString()
                , Session["ClassSeq"].ToString()
                , txtDescr.Text
                , "", ssMng.EmpSeqI);
            LoadDuty();
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            dutyBLL.Update(Int32.Parse(lblSeq.Text.Trim())
                 , Session["FieldSeq"].ToString()
                 , Session["ClassSeq"].ToString()
                 , txtDescr.Text
                 , "", ssMng.EmpSeqI);
            LoadDuty();
        }

     }
}
